
#include "spi2.h"
#include "config.h"
void WIZ_SPI_Init(void)
{
 
   /* USER CODE BEGIN SPI2_Init 0 */
 
   /* USER CODE END SPI2_Init 0 */
 
   /* USER CODE BEGIN SPI2_Init 1 */
 
   /* USER CODE END SPI2_Init 1 */
   /* SPI2 parameter configuration*/
   hspi2.Instance = SPI2;
   hspi2.Init.Mode = SPI_MODE_MASTER;
   hspi2.Init.Direction = SPI_DIRECTION_2LINES;
   hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
   hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
   hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
   hspi2.Init.NSS = SPI_NSS_SOFT;//SPI_NSS_HARD_OUTPUT;
   hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
   hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
   hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
   hspi2.Init.CRCPolynomial = 10;
   if (HAL_SPI_Init(&hspi2) != HAL_OK)
   {
	 Error_Handler();
   }
   /* USER CODE BEGIN SPI2_Init 2 */
   SPI2->CR1 |= SPI_CR1_SPE;  // 使能SPI2
   /* USER CODE END SPI2_Init 2 */
}
void WIZ_CS(uint8_t val)
{
	if (val == LOW) {
		HAL_GPIO_WritePin(GPIOB, WIZ_SCS,GPIO_PIN_RESET); 
	}else if (val == HIGH){
		HAL_GPIO_WritePin(GPIOB, WIZ_SCS,GPIO_PIN_SET); 
	}
}


uint8_t SPI2_SendByte(uint8_t byte)
{
	while (!(SPI2->SR & SPI_SR_TXE));  // Wait until TX buffer empty
    SPI2->DR = byte;    
	// Send byte
	
    while (!(SPI2->SR & SPI_SR_RXNE));  // Wait until RX buffer not empty
    return SPI2->DR;                    // Return received byte
}
/*
void SPI1_TXByte(uint8_t byte)
{
	  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);       

	  SPI_I2S_SendData(SPI1, byte);	
}
*/

